package com.jintu.portal.service.datadriver.parser.impl;

import com.alibaba.fastjson.JSONObject;
import com.jintu.portal.pojo.submit.DataResponse;
import com.jintu.portal.service.datadriver.parser.DataParser;
import com.jintu.portal.service.datadriver.parser.ParserCommon;
import com.jintu.portal.service.datadriver.pojo.MsgRespCode;
import com.jintu.portal.service.datadriver.pojo.RespInfo;
import com.jintu.portal.service.datadriver.pojo.Response;
import com.jintu.portal.service.datadriver.pojo.SysRespCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * @author very
 */
@Slf4j
@Component
public class ZmScoreParser implements DataParser {

    @Override
    public boolean parseAndSaveData(String data, DataResponse dataResponse, Integer userId) {
        if (StringUtils.isBlank(data)) {
            return ParserCommon.isDataSourceIsEmpty(dataResponse);
        }
        Response response = JSONObject.parseObject(data, Response.class);
        String retCode = response.getRetCode();
        RespInfo respInfo = response.getRetInfo();
        if (SysRespCode.QUERY_SUCCESS.getCode().equals(retCode)
                && MsgRespCode.SEARCH.getCode().equals(respInfo.getCode())) {
            ParserCommon.wrapAuthSuccess(dataResponse, response);
        } else {
            dataResponse.setResponseJson(JSONObject.parseObject(JSONObject.toJSONString(response)));
        }
        return false;
    }
}
